package com.cqfan.cloud.office.console.controller;

import cn.hutool.core.io.IoUtil;
import com.cqfan.cloud.office.libreoffice.service.ConverterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.office.OfficeException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author JiaLing.Fan
 * @version 1.0
 * @date 2024-05-12 13:13
 * @description ConverterController
 */

@Controller
@RequestMapping(value = "/converter")
@Slf4j
public class ConverterController {

    @Resource
    private ConverterService converterService;


    @RequestMapping(value = "/docxToPdf" ,method = RequestMethod.PUT)
    @ResponseBody
    public void docxToPdf(@RequestParam("file") MultipartFile file , HttpServletResponse response) {
        try {
            InputStream inputStream =  file.getInputStream();
            OutputStream outputStream = response.getOutputStream();
            converterService.convert(inputStream , outputStream , "pdf");
            IoUtil.writeUtf8(outputStream, true);
        } catch (Exception e) {
            log.error(e.getMessage() , e);
        }
    }
}
